﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RetailAnalytic.Core
{
    public class HistoryUser
    {
        private bool writeToFile;
        private string FileUsrLog;

        private User _user;
        private string _versionBuild;
        public HistoryUser(User user,string VersionBuild)
        {
            this.FileUsrLog = Application.StartupPath + "\\FileUsrLog.log";
            this._user = user;
            this._versionBuild = VersionBuild;
            this.writeToFile = true;
        }
        public void WriteLog(RetailAnalyticEnum.Event_Type sEvent, string typePresent, Exception ex)
        {
            string exMessage = " Internal Error : Message: {0}" + ex.Message + "StackTrace: {1} " + ex.StackTrace + " Source: {2}" + ex.Source;
            exMessage = String.Format(exMessage, ex.Message, ex.StackTrace, ex.Source);
            WriteLog(-1, sEvent, typePresent, exMessage);
        }
        public void WriteLog(RetailAnalyticEnum.Event_Type sEvent, string typePresent)
        {
            WriteLog(-1, sEvent, typePresent, "");
        }
        public void WriteLog(Int32 classTaskID, RetailAnalyticEnum.Event_Type sEvent, string typePresent, string sExtend)
        {
            UsrLog newEvent = new UsrLog();
            newEvent.UsrLog_Uid = this._user.userID;
            newEvent.UsrLog_dateTime = System.DateTime.Now;
            newEvent.UsrLog_session = SessionConfiguration.IDSession;
            newEvent.UsrLog_EventType = sEvent + "  " + typePresent;
            newEvent.UsrLog_ClassTaskID = classTaskID;
            newEvent.UsrLog_Status = 0;
            newEvent.UsrLog_extended = sExtend;
            newEvent.UsrLog_host = System.Net.Dns.GetHostName();
            newEvent.UsrLog_version = this._versionBuild;
            if (writeToFile)
            {
                bool existFile = (File.Exists(this.FileUsrLog)) ? false : true;
                StreamWriter SW = new StreamWriter(this.FileUsrLog, true);
                if(existFile)
                {
                    SW.WriteLine("UsrLog_Uid" + "\t" +
                        "UsrLog_dateTime" + "\t" +
                        "UsrLog_session" + "\t" +
                        "UsrLog_EventType" + "\t" +
                        "UsrLog_ClassTaskID" + "\t" +
                        "UsrLog_Status" + "\t" +
                        "UsrLog_extended" + "\t" +
                        "UsrLog_host" + "\t" +
                        "UsrLog_version");
                }
                
                SW.WriteLine(newEvent.UsrLog_Uid + "\t" +
                    newEvent.UsrLog_dateTime + "\t" +
                    newEvent.UsrLog_session + "\t" +
                    newEvent.UsrLog_EventType + "\t" +
                    newEvent.UsrLog_ClassTaskID + "\t" +
                    newEvent.UsrLog_Status + "\t" +
                    newEvent.UsrLog_extended + "\t" +
                    newEvent.UsrLog_host + "\t" +
                    newEvent.UsrLog_version);
                SW.Close();
            }
                GRetailAnalytic.Instance.radb.UsrLogs.InsertOnSubmit(newEvent);
                GRetailAnalytic.Instance.radb.SubmitChanges();
        }
    }
}
